#!/usr/bin/env python
# -*- coding: utf-8 -*

import clutter
from clutter import Stage, Group, Rectangle, Text
from icon import ActionIcon
from iconbar import IconBar
from infobar import InfoBar
from widgets_area import WidgetsArea
from gstmanager.event import EventListener
from clutter import Texture

import touchwizard

import logging, sys

class Canvas(EventListener):
    def __init__(self, width=800, height=480, title="Default canvas"):
        EventListener.__init__(self)
        self.stage = stage = Stage()
        stage.set_title(title)

        self.swidth = width
        self.sheight = height
        self.stage.set_size(width, height)

        self.build_info_bar()
        self.build_icon_bar()
        self.build_widgets_area()
        self.build_background()

        self.registerEvent("quit")

    def evt_quit(self, event):
        if hasattr(self, "stop_main_loop"):
            self.stop_main_loop()
        else:
            clutter.main_quit()

    def build_background(self):
        if touchwizard.background_path is not None:
            back = Texture(touchwizard.background_path)
            back.show()
            self.stage.add(back)
            back.lower_bottom()

    def build_info_bar(self):
        self.info_bar = info_bar = InfoBar()
        self.stage.add(info_bar)
        info_bar.set_position(0, 0)

    def build_widgets_area(self):
        height = self.sheight-self.info_bar.get_height()-self.icon_bar.get_height() + 5
        self.widgets_area = widgets_area = WidgetsArea(self.swidth, height)
        widgets_area.set_position(0, self.info_bar.get_height())
        self.stage.add(widgets_area)

    def build_icon_bar(self):
        self.icon_bar = icon_bar = IconBar(width=self.swidth, height=120)
        self.stage.add(icon_bar)
        icon_bar.set_position(0, self.sheight - icon_bar.get_height())

    def add_icon_raw(self, icon):
        self.icon_bar.add_icon_raw(icon)

    def add_icon(self, **kwargs):
        icon = ActionIcon(**kwargs)
        self.icon_bar.add_icon_raw(icon)

    def add_widget(self, widget):
        self.widgets_area.add(widget)
        widget.show()

    def add_widget_raw(self, widget):
        self.widgets_area.add(widget)
        widget.show()

    def remove_icon_by_event_name(self, event_name):
        self.icon_bar.remove_icon_by_event_name(event_name)

    def get_widgets_height(self):
        height = self.sheight-self.icon_bar.get_height() - self.info_bar.get_height() - 5
        return height

    def run(self, start_main_loop=None, stop_main_loop=None):
        self.stage.show_all()
        if start_main_loop is not None and stop_main_loop is not None:
            self.stop_main_loop = stop_main_loop
            start_main_loop()
        else:
            clutter.main()

if __name__ == '__main__':

    logging.basicConfig(
        level=getattr(logging, "DEBUG"),
        format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
        stream=sys.stderr
    )

    width = 1024
    height = 768
    max_icons = 6

    t = Canvas(width, height, "Demo app")
    t.add_icon(file="trash", launch_evt="info", desc="Trash", action_args="Test info")
    t.add_icon(file="exit", launch_evt="quit", desc="Exit", cooldown_ms=2000)

    from icon import ActionIconToggle
    i = ActionIconToggle(file="trash", file_back="player_stop", desc="LocksOthers", lock_others=True, launch_evt="test_action")
    t.add_icon_raw(i)

    r = Rectangle(clutter.color_from_string("Blue"))
    r.set_size(500, 200)
    t.add_widget(r)

    r = Rectangle(clutter.color_from_string("Yellow"))
    r.set_size(300, 200)
    t.add_widget(r)

    loop = max_icons - 3
    print "trying to display %s icons" %max_icons
    for i in range(loop):
        t.add_icon(file="stop", launch_evt="stop", desc="Stop", cooldown_ms=2000)
    t.run()
